package com.managedBean.merchant;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import com.controller.user.MyAccountController;
import com.dto.GooglemapsDTO;
import com.dto.merchant.UserDTO;

@ManagedBean(name = "mapsManagedBean")
@ViewScoped
public class MapsManagedBean implements Serializable {

	private static final long serialVersionUID = 5374183506990537751L;
	// G Map code starts here
	private MapModel emptyModel;
	private String title;
	@ManagedProperty(value = "#{myAccountController}")
	private MyAccountController myAccountController;
	public List<GooglemapsDTO> list;
	public Marker marker;
	public UserDTO userDTO;
	@PostConstruct
	public void init() {
		try {
			// GMap Code
			emptyModel = new DefaultMapModel();
			list = myAccountController.findAll();
			for (GooglemapsDTO dto : list) {
				marker = new Marker(new LatLng(new Double(dto.getLat()),
						new Double(dto.getLang())), dto.getUserDTO()
						.getMobileno(), dto.getUserDTO());
				emptyModel.addOverlay(marker);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void onMarkerSelect(OverlaySelectEvent event) {
		marker = (Marker) event.getOverlay();
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Marker Selected",(((UserDTO) marker.getData()).getMobileno())));
		userDTO=((UserDTO) marker.getData());
	}
	public void addMessage(FacesMessage message) {
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	/**
	 * @return the emptyModel
	 */
	public MapModel getEmptyModel() {
		return emptyModel;
	}

	/**
	 * @param emptyModel
	 *            the emptyModel to set
	 */
	public void setEmptyModel(MapModel emptyModel) {
		this.emptyModel = emptyModel;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the myAccountController
	 */
	public MyAccountController getMyAccountController() {
		return myAccountController;
	}

	/**
	 * @param myAccountController
	 *            the myAccountController to set
	 */
	public void setMyAccountController(MyAccountController myAccountController) {
		this.myAccountController = myAccountController;
	}
	/**
	 * @return the list
	 */
	public List<GooglemapsDTO> getList() {
		return list;
	}
	/**
	 * @param list the list to set
	 */
	public void setList(List<GooglemapsDTO> list) {
		this.list = list;
	}
	/**
	 * @return the marker
	 */
	public Marker getMarker() {
		return marker;
	}
	/**
	 * @param marker the marker to set
	 */
	public void setMarker(Marker marker) {
		this.marker = marker;
	}
	/**
	 * @return the userDTO
	 */
	public UserDTO getUserDTO() {
		return userDTO;
	}
	/**
	 * @param userDTO the userDTO to set
	 */
	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
}
